package com.create.vip.mall.brand.controller;

import com.create.vip.mall.brand.model.Brand;
import com.create.vip.mall.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.create.mall.util.RespResult;

import java.util.List;

@RestController
@RequestMapping(value = "/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 增加方法
     * 一个请求，只有一个RequestBody；一个请求，可以有多个RequestParam
     * 如果参数时放在请求体中，application/json传入后台的话，那么后台要用@RequestBody才能接收到；
     * 如果不是放在请求体中的话，那么后台接收前台传过来的参数时，要用@RequestParam来接收，或则形参前 什么也不写也能接收。
     */
    @PostMapping
    public RespResult add(@RequestBody Brand brand){
        brandService.save(brand);
        return RespResult.ok();
    }

    /**
     * 修改方法
     */
    @PutMapping
    public RespResult update(@RequestBody Brand brand){
        brandService.updateById(brand);
        return RespResult.ok();
    }

    @DeleteMapping("/{id}")
    public RespResult delete(@PathVariable(value = "id") String id){
        brandService.removeById(id);
        return RespResult.ok();
    }

    @GetMapping(value = "/query")
    public RespResult query(@RequestBody Brand brand){
        RespResult respResult = new RespResult();
        respResult.setData(brandService.queryList(brand));
        respResult.setCode(20000);
        return respResult;
    }

    @GetMapping(value = "/query/pageSize")
        public RespResult<List<Brand>> queryPage(@RequestBody Brand brand, @RequestParam(value = "page") int page, @RequestParam(value = "pageSize")int pageSize){
        RespResult respResult = new RespResult();
        respResult.setData(brandService.queryPageList(brand, (long) page, (long) pageSize));
        respResult.setCode(20000);
        return respResult;
    }
}
